
/*
    tokenLogin
    使用token进行自动登录
    token暂时只是服务端生成的随机字符串，存在cookie中，未进行更多加密处理
 */
function tokenLogin() {
    console.log("tokenLoginsss");
    let token = getCookieValue('token');
    if (token != null) {
        let url = '/StudentsManager/tokenLogin';
        const net = new NetClient();
        net.requestUrl(url, 'POST', {}, (error, response) => {
            if (error) {
                console.log(error);
            }
            else {
                if (response.errorCode === 0) {
                    //登录成功以后才跳转页面
                    window.location.href = "/StudentsManager/welcome.html";
                }
            }
        });
    }
    else {
        console.log("没有cookie token");
    }
}

//登录操作
function mySubmit() {
    console.log("提交事件。。。sss");
    var username = document.getElementById("userName").value;
    var password = document.getElementById("password").value;
    password = md5(password);
    let url = '/StudentsManager/login.do';
    const net = new NetClient();
    net.requestUrl(url, 'POST', {"userName":username, "password":password}, (error, response) => {
        if (error) {
            console.log(error);
        }
        else {
            if (response.errorCode === 0) {
                //登录成功以后才跳转页面
                console.log("登录成功");
                window.location.href = "/StudentsManager/welcome.html";
            }
            else {
                console.log("登录失败");
                showLoginFailTip();
            }
        }
    });
    return false
}

// 在用户登录失败后在页面上显示提示信息
function showLoginFailTip() {
    // 获取登录失败提示信息容器
    var tipBox = document.getElementById('error-message');

    // 显示提示信息容器，并设置提示文本
    tipBox.style.display = 'block';
    tipBox.innerHTML = '用户名或密码错误，登录失败。';

    // 设置提示信息容器的样式
    tipBox.style.color = 'red';
    tipBox.style.textAlign = 'center';
    tipBox.style.padding = '10px';
}

function getCookieValue(cookieName) {
    const name = cookieName + "=";
    const decodedCookie = decodeURIComponent(document.cookie);
    const cookieArray = decodedCookie.split(';');
    for(let i = 0; i < cookieArray.length; i++) {
        let cookie = cookieArray[i].trim();
        if (cookie.indexOf(name) === 0) {
            return cookie.substring(name.length, cookie.length);
        }
    }
    return null;
}